//
//  IMNetworkUtilsTests.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/6/23.
//

#import <XCTest/XCTest.h>
#import "IMNetworkUtils.h"

@interface IMNetworkUtilsTests : XCTestCase
@property (nonatomic, strong) IMNetworkConfig *testConfig;
@end

@implementation IMNetworkUtilsTests

- (void)setUp {
    [super setUp];
    
    // 设置测试配置
    self.testConfig = [IMNetworkConfig defaultConfig];
    self.testConfig.baseURL = @"https://jsonplaceholder.typicode.com";
    self.testConfig.timeoutInterval = 10.0;
    self.testConfig.enableLogging = YES;
    
    // 初始化网络工具
    [IMNetworkUtils initWithConfig:self.testConfig];
}

- (void)tearDown {
    [IMNetworkUtils cancelAllRequests];
    [super tearDown];
}

- (void)testNetworkConfiguration {
    // 测试默认配置
    IMNetworkConfig *defaultConfig = [IMNetworkConfig defaultConfig];
    XCTAssertNotNil(defaultConfig);
    XCTAssertEqual(defaultConfig.timeoutInterval, 30.0);
    XCTAssertTrue(defaultConfig.enableLogging);
    
    // 测试配置更新
    [IMNetworkUtils updateConfig:defaultConfig];
    IMNetworkConfig *currentConfig = [IMNetworkUtils currentConfig];
    XCTAssertEqual(currentConfig.timeoutInterval, defaultConfig.timeoutInterval);
}

- (void)testGETRequest {
    XCTestExpectation *expectation = [self expectationWithDescription:@"GET请求"];
    
    [IMNetworkUtils GET:@"/posts/1"
             parameters:nil
                success:^(IMNetworkResponse *response) {
                    XCTAssertNotNil(response);
                    XCTAssertEqual(response.statusCode, 200);
                    XCTAssertNotNil(response.data);
                    XCTAssertNil(response.error);
                    [expectation fulfill];
                }
                failure:^(IMNetworkResponse *response) {
                    XCTFail(@"GET请求失败: %@", response.error.localizedDescription);
                    [expectation fulfill];
                }];
    
    [self waitForExpectationsWithTimeout:10.0 handler:nil];
}

- (void)testPOSTRequest {
    XCTestExpectation *expectation = [self expectationWithDescription:@"POST请求"];
    
    NSDictionary *params = @{
        @"title": @"测试标题",
        @"body": @"测试内容",
        @"userId": @1
    };
    
    [IMNetworkUtils POST:@"/posts"
              parameters:params
                 success:^(IMNetworkResponse *response) {
                     XCTAssertNotNil(response);
                     XCTAssertEqual(response.statusCode, 201);
                     XCTAssertNotNil(response.data);
                     [expectation fulfill];
                 }
                 failure:^(IMNetworkResponse *response) {
                     XCTFail(@"POST请求失败: %@", response.error.localizedDescription);
                     [expectation fulfill];
                 }];
    
    [self waitForExpectationsWithTimeout:10.0 handler:nil];
}

- (void)testNetworkStatus {
    NSString *status = [IMNetworkUtils networkStatus];
    XCTAssertNotNil(status);
    
    NSArray *validStatuses = @[@"Unknown", @"NotReachable", @"ReachableViaWWAN", @"ReachableViaWiFi"];
    XCTAssertTrue([validStatuses containsObject:status]);
}

- (void)testTaskCancellation {
    IMNetworkTask *task = [IMNetworkUtils GET:@"/posts"
                                   parameters:nil
                                      success:nil
                                      failure:nil];
    
    XCTAssertNotNil(task);
    XCTAssertFalse(task.isCancelled);
    
    [task cancel];
    XCTAssertTrue(task.isCancelled);
}

- (void)testActiveRequestCount {
    XCTAssertEqual([IMNetworkUtils activeRequestCount], 0);
    
    [IMNetworkUtils GET:@"/posts/1" parameters:nil success:nil failure:nil];
    XCTAssertEqual([IMNetworkUtils activeRequestCount], 1);
    
    [IMNetworkUtils cancelAllRequests];
    XCTAssertEqual([IMNetworkUtils activeRequestCount], 0);
}

- (void)testNetworkStatistics {
    NSDictionary *stats = [IMNetworkUtils networkStatistics];
    XCTAssertNotNil(stats);
    XCTAssertNotNil(stats[@"totalRequests"]);
    XCTAssertNotNil(stats[@"successfulRequests"]);
    XCTAssertNotNil(stats[@"failedRequests"]);
}

@end 